<?php 
    include('../../header.php');

    $action=$_GET['action'];
    if($action == 'list'){

        include('../../connect.php');//链接数据库
        $q="select * from announce";
        $result=mysql_query($q,$con);//执行sql
        $rows=mysql_num_rows($result);//返回一个数值  0 false 1 true
    
        if($rows){
            for($i=0;$i<$rows;$i++){
                $res['data'][$i] = mysql_fetch_assoc($result);/*循环拿到数据库中所有结果 */
            }
               $res['status'] = 1;
               echo json_encode($res);
        }else{
            echo json_encode($res);
            die();
        }
        mysql_close($con);

    }else if($action == 'add'){

        $belong=$_POST['belong'];
        $title=$_POST['title'];
        $content=$_POST['content'];
        $attachment=$_POST['attachment'];
        $createAt = date("Y-m-d h:i:s");
        if( !($belong && $title && $content) ){
            $res['msg']['en'] = 'Error: Incomplete information';
            $res['msg']['cn'] = 'Error: 信息填写不完整';
            echo json_encode($res);
            die();
        }
        include('../../connect.php');
        $q="insert into announce(belong,title,content,attachment,createAt) values ('$belong','$title','$content','$attachment','$createAt')";//向数据库插入表单传来的值的sql
        $reslut=mysql_query($q,$con);
        if (!$reslut){
            $res['msg']['en'] = 'Error: Error occurred'.  mysql_error();
            $res['msg']['cn'] = 'Error: 出现错误'.  mysql_error();
            echo json_encode($res);
            die();
        }else{
            $res['status'] = 1;
        }
        echo json_encode($res);
        mysql_close($con);

    }else if($action == 'detail'){

    }


    
?>